package com.tanfuqi.sicent.server.controller

import com.tanfuqi.sicent.protocol.message.Message
import com.tanfuqi.sicent.protocol.message.Recharge
import com.tanfuqi.sicent.server.filter.sent
import io.swagger.annotations.ApiOperation
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import reactor.core.publisher.toMono
@RestController
class RechageController :BaseController(){

    @ApiOperation(value = "充值")
    @PostMapping("/shop/{shopId}/member/{cardId}/recharge")
    @ResponseStatus(HttpStatus.CREATED)
    fun recharge(@PathVariable shopId: String, @PathVariable cardId: String, @RequestBody recharge: Recharge): Mono<Any> =
            recharge.toMono().doOnSuccess {
                it.cardId = cardId
                it.allowReward = 1
            }.flatMap { recharge ->
                session(shopId).sent(Message(0x804A, body = recharge)).map { it.body }
            }

    @ApiOperation(value = "扣费")
    @PostMapping("/shop/{shopId}/member/{cardId}/refund")
    @ResponseStatus(HttpStatus.CREATED)
    fun refund(@PathVariable shopId: String, @PathVariable cardId: String, @RequestBody recharge: Recharge): Mono<Any> =
            recharge.toMono().doOnSuccess {
                it.cardId = cardId
                it.principal = it.amount
                it.amount = null
            }.flatMap { recharge ->
                session(shopId).sent(Message(0x804C, body = recharge)).map { it.body }
            }
}